//
//  AppDelegate.swift
//  XJWeiBo
//
//  Created by 徐健 on 16/3/6.
//  Copyright © 2016年 徐健. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        
        setThemeColor()
        
        //调用注册的通知
        registerNotification()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
//        window?.rootViewController = welcomeViewController()
        getRootViewController()
        
        
        window?.makeKeyAndVisible()
        

        return true
    }
    
    
    //通知监听的方法的
    
    @objc private func changeRootVC(n:NSNotification){
        window?.rootViewController = (n.object == nil) ? MainViewController():welcomeViewController()
    }
    
    
    
    //注册通知
    private func registerNotification(){
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootVC:", name: switchRootViewController, object: nil)
    }
    
    
    
    //判断用户是否登录
    private func getRootViewController(){
        let rootVC = userAccount.onlyUserAccount.userLogin ? welcomeViewController():MainViewController()
        
        
        window?.rootViewController = rootVC
    }
    
    
    private func setThemeColor(){
        UINavigationBar.appearance().tintColor = themeColor
        
        UITabBar.appearance().tintColor = themeColor
    }

    
    //移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

}

